package com.example.water_quality_analysis.job;

import com.example.water_quality_analysis.mapper.DataMapper;
import com.example.water_quality_analysis.pojo.model.Data;
import com.example.water_quality_analysis.pojo.model.DataDoc;
import jakarta.annotation.Resource;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class DataBackupJob implements Job {

    @Resource
    private ElasticsearchTemplate elasticsearchTemplate;
    @Resource
    private DataMapper dataMapper;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        List<Data> data = dataMapper.selectList(null);
        List<DataDoc> dataDocList = data.stream().map(DataDoc::new).toList();
        elasticsearchTemplate.save(dataDocList);
    }
}
